package com.controller;

import com.api.CommonResult;
import com.entity.BType;
import com.entity.FUser;
import com.entity.table.BUserTableDef;
import com.entity.table.FUserTableDef;
import com.mapper.BUserMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.BUser;
import com.service.BUserService;

import java.io.Serializable;
import java.util.List;


/**
 * 管理员表 控制层。
 *
 * @author admin
 * @since 2024-05-17
 */
@Controller
@RequestMapping("/admin")
public class BUserController {

    @Autowired
    private BUserService bUserService;

    @GetMapping (value= "/adminList")
    public String userList(ModelMap model, @RequestParam(value= "adminName", defaultValue ="" )String adminName,
                           @RequestParam(value ="p", defaultValue ="1") int p,
                           @RequestParam(value ="pageSize", defaultValue ="10") int pageSize) {
        //按照用户名模查询
        QueryWrapper queryWrapper = QueryWrapper.create().where(BUserTableDef.B_USER.USERNAME.like(adminName));
        Page<BUser> page = new Page<>();
        page.setPageSize(pageSize);//设每显示记录数
        page.setPageNumber(p);
        Page<BUser> pageInfo = bUserService.page(page, queryWrapper);
        model.addAttribute("page", pageInfo);
        model.addAttribute("total", pageInfo.getTotalRow());
        return "back/admin-list";
    }


    //添加会员页面代码
    @GetMapping(value ="/addView")
    public String addUserView() {return "back/admin-add";}
    /**
     * 添加管理员表。
     *
     * @param bUser 管理员表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ResponseBody
    public CommonResult save(BUser bUser) {
        try {
            boolean r = bUserService.save(bUser);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = bUser.getUsername()+"已存在";
            return CommonResult.failed(msg);
        }
    }

    /**
     * 根据主键删除管理员表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return bUserService.removeById(id);
    }


    //添加修改管理员页面代码
    @GetMapping(value = "updateView")
    public String updateView(int id, ModelMap model){
        BUser bUser1=bUserService.getById(id);
        model.addAttribute("admin",bUser1);
        return "back/admin-edit";
    }
    /**
     * 根据主键更新管理员表。
     *
     * @param bUser 管理员表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PostMapping("update")
    @ResponseBody
    public CommonResult update( BUser bUser) {
        try {
            boolean r =bUserService.updateById(bUser);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = bUser.getUsername()+"已存在";
            return CommonResult.failed(msg);
        }
    }
//添加删除用户代码
    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id){
        try{
            boolean r=bUserService.removeById(id);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();

            return CommonResult.failed(e.getMessage());
        }
    }
    /**
     * 查询所有管理员表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<BUser> list() {
        return bUserService.list();
    }

    /**
     * 根据管理员表主键获取详细信息。
     *
     * @param id 管理员表主键
     * @return 管理员表详情
     */
    @GetMapping("getInfo/{id}")
    public BUser getInfo(@PathVariable Serializable id) {
        return bUserService.getById(id);
    }

    /**
     * 分页查询管理员表。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<BUser> page(Page<BUser> page) {
        return bUserService.page(page);
    }
//    @Autowired
//     BUserMapper bUserMapper;

    @GetMapping("/login") // 显示登录页面
    public String toLoginPage() {
        return "back/Login";
    }

    @PostMapping("/toLogin") // 提交登录，登录成功跳转至后台首页
    public String login(BUser bUser, HttpSession httpSession, Model model) {
//        BUser bUser1 = bUserMapper.userLogin(bUser);
        QueryWrapper query=QueryWrapper.create()
                .where(BUserTableDef.B_USER.USERNAME.eq(bUser.getUsername()))
                .and(BUserTableDef.B_USER.PASSWORD.eq(bUser.getPassword()));
        BUser bUser1=bUserService.getOne(query);
        if (bUser1!= null) {
            httpSession.setAttribute("admin", bUser1); // 将用户对象添加到 Session
            model.addAttribute("admin", bUser1);
            return "back/index";
        } else {
            model.addAttribute("msg", "账号或密码错误，请重新输入！");
            return "back/login";
        }
    }
    @GetMapping(value = "ue")
    public String ue(Model model){return "index";}
}
